import { Component, OnInit, ViewChild } from '@angular/core';
import { ApiService } from './../../../../core/api/api.service';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService, NzTreeComponent } from 'ng-zorro-antd';
import { ActivatedRoute } from '@angular/router';

@Component({
    templateUrl: './departmentUpdate.html',
    styles: [
        `
          [nz-button] {
            margin-right: 8px;
            margin-bottom: 12px;
          }
        `
      ]
})
export class DepartmentUpdateComponent implements OnInit {
    @ViewChild('permissionTree') permissionTree: NzTreeComponent;
    department: any = {}; // 部门详情
    id;
    permissions = []; // 权限树
    permissionList = []; // 权限列表
    selectPermission = []; // 已选中权限
    departmentTypesList = []; // 科室类别列表
    departmentTypes = {}; // 科室类别map
    validateForm;
    constructor(
        private apiService: ApiService,
        private fb: FormBuilder,
        private message: NzMessageService,
        private route: ActivatedRoute
        ) {
    }
    checked(e) {
    }
    savePermission() {
        const array = this.permissionTree.getTreeNodes();
        const checkedKeys = [];
        this.getTreeNodeKey(array, checkedKeys);
        this.apiService.post('保存医院科室分配权限',{departmentId:this.id,permissions:checkedKeys}).then(res=>{
            this.message.success('科室权限保存成功！');
        })
    }
    updateDepartment() {
        this.apiService.post('编辑医院科室', {departmentId: this.id, departmentTypeId:this.validateForm.controls.departmentTypeId.value, status: this.validateForm.controls.status.value}).then((res: any) => {
            this.message.success('状态修改成功！');
        });
    }
    getTreeNodeKey(array, value) {
        array.forEach(element => {
            if (element.isChecked || element.isHalfChecked) {
                value.push(element.key);
            }
            if (element.children && element.children.length > 0) {
                this.getTreeNodeKey(element.children, value);
            }
        });
    }
    ngOnInit() {
        this.route.params.subscribe(p => this.id = p.id);
        this.apiService.post('医院科室类别列表').then((res: any) => {
            this.departmentTypesList = res. data.list;
            res.data.list.forEach(element => {
                this.departmentTypes[element.id] = element.name;
            });
        });
        this.apiService.post('医院科室分配权限详情', {departmentId: this.id}).then((res: any) => {
            this.department = res.data.hospitalDepartment;
            this.selectPermission = this.department.permission ? this.department.permission.split(',') : [];
            const {departmentName, departmentTypeId, memo, status, hospitalName, medicalCode} = this.department;
            this.validateForm.setValue({departmentName, departmentTypeId, memo, status, hospitalName, medicalCode});
            this.permissionList = res.data.path;
            this.permissions = this.listByTree(res.data.path);
            console.log(this.permissions);
        });
        this.validateForm = this.fb.group({
            departmentName: [null, [Validators.required]],
            departmentTypeId: [null, [Validators.required]],
            memo: [null],
            status: [null],
            hospitalName: [null],
            medicalCode: [null],
        });
    }
    listByTree(list) {
        const tree = [], treeObj = {};
        list.forEach(i => {
            i.key = i.permission;
            i.title = i.name;
            treeObj[i.id] = i;
            if (i.parentId === 0) {
                tree.push(i);
            }
        });
        list.forEach(i => {
            if (treeObj[i.parentId]) {
                treeObj[i.parentId].children = treeObj[i.parentId].children || [];
                treeObj[i.parentId].children.push(i);
            }
        });
        return tree;
    }
}
